<template>
  <div>
    <div class="form">
      <div class="search">
        <div class="date_picker">
          <el-input
            placeholder="班级名称"
            v-model="className"
            class="search_info"
          >
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
          <el-select v-model="classType" placeholder="请选择班级类型">
            <el-option
              v-for="item in classList"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            ></el-option>
          </el-select>
        </div>
        <div>
          <el-button @click="reset()" class="searchBtn">重置</el-button>
          <el-button type="primary" @click="getList(1)" class="searchBtn"
            >搜索</el-button
          >
          <el-button type="primary" @click="add()" class="searchBtn"
            >新增班级</el-button
          >
        </div>
      </div>
      <el-table
        class="listTable"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="班级名称" prop="id" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.className }}</span>
          </template>
        </el-table-column>
        <el-table-column label="班级类型" width="100" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.classTypes }}</span>
          </template>
        </el-table-column>
        <el-table-column label="做题类型" width="100" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.typeValue }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开班时间" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.classStartTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="毕业时间" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.classEndTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="责任教员" width="100" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.teacher }}</span>
          </template>
        </el-table-column>
        <el-table-column label="报名人数" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.signUpNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程名称" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.courseNames }}</span>
          </template>
        </el-table-column>
        <el-table-column label="赠送列表" show-overflow-tooltip align="center">
          <template slot-scope="{ row }">
            <span>{{ row.presentGoodsName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="240"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="give(scope.row.classId)"
              >赠送</el-button
            > -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="update(scope.row.classId)"
              v-hasPermi="['financeProduct:product:edit']"
              >商品更新</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="preview(scope.row.classId)"
              v-hasPermi="['financeProduct:product:edit']"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['financeProduct:product:remove']"
              @click="edit(scope.row.classId)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="del(scope.row.classId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        layout="total, sizes, prev, pager, next"
        :page-size="limit"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="handleSizeChange"
        :current-page.sync="curPage"
        @current-change="getList($event)"
        class="sortStyle"
      />
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      title="赠送商品选择"
      width="648px"
      height="150px"
    >
      <el-select
        v-model="goodsId"
        placeholder="请选择赠送的商品"
        multiple
        style="width: 400px; margin-bottom: 16px"
      >
        <el-option
          v-for="item in goodsList"
          :key="item.id"
          :label="item.titleName"
          :value="item.id"
        ></el-option>
      </el-select>
      <div class="footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="save"> 保存 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  classDel,
  studentOpen,
  studentStop,
  classList,
  getGoodsList,
  presentCourse,
  classPresentGoods
} from "@/api/system/user";
import {} from "@/api/article";
export default {
  components: {},
  filters: {
    typeValue: function (value) {
      // let temp = "";
      // value.forEach((e) => {
      //   switch (e) {
      //     case 1:
      //       temp += "课后作业,";
      //       break;
      //     case 2:
      //       temp += "押题模考,";
      //       break;
      //     case 3:
      //       temp += "结业考试,";
      //       break;
      //     case 4:
      //       temp += "智能练习,";
      //       break;
      //   }
      // });
      // return temp;
    },
  },
  data() {
    return {
      dialogVisible: false,
      list: [],
      listLoading: false,
      total: 0,
      limit: 10,
      curPage: 1,
      listData: [],
      studentName: "",
      phonenumber: "",
      className: "",
      classType: "",
      classList: [
        { key: "1", value: "初级" },
        { key: "2", value: "中级" },
        { key: "3", value: "高级" },
      ],
      classId: null,
      goodsList: [],
      goodsId: [],
    };
  },
  created() {},
  mounted() {
    this.getList(1);
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      getGoodsList().then((res) => {
        if (res.code == 200) {
          this.goodsList = res.rows;
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    give(id) {
      this.dialogVisible = true;
      this.classId = id;
    },
    save() {
      let params = {
        classId: this.classId,
        goodsId: this.goodsId,
      };
      presentCourse(params).then((res) => {
        this.dialogVisible = false;
        if (res.code == 200) {
          this.$message({ message: res.msg, type: "success" });
          this.getList(1);
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    stop(id) {
      studentStop(id).then((res) => {
        if (res.code == 200) {
          this.$message({ message: res.msg, type: "success" });
          this.getList(1);
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    open(id) {
      studentOpen(id).then((res) => {
        if (res.code == 200) {
          this.$message({ message: res.msg, type: "success" });
          this.getList(1);
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    reset() {
      this.className = "";
      this.classType = "";
    },
    /** 查询用户列表 */
    getList(page) {
      let params = {
        pageNum: page,
        pageSize: this.limit,
        className: this.className,
        classType: this.classType,
      };
      this.listLoading = true;

      // let temp = "";
      // value.forEach((e) => {
      //   switch (e) {
      //     case 1:
      //       temp += "课后作业,";
      //       break;
      //     case 2:
      //       temp += "押题模考,";
      //       break;
      //     case 3:
      //       temp += "结业考试,";
      //       break;
      //     case 4:
      //       temp += "智能练习,";
      //       break;
      //   }
      // });
      // return temp;

      classList(params).then((res) => {
        this.listLoading = false;
        if (res.code == 200) {
          this.list = res.rows;
          this.total = res.total;
          this.list.forEach((element) => {
            if (element.classType == "1") {
              element.classTypes = "初级";
            } else if (element.classType == "2") {
              element.classTypes = "中级";
            } else {
              element.classTypes = "高级";
            }
            var temp = "";
            for (let i = 0; i < element.questionTypeIds.length; i++) {
              switch (element.questionTypeIds[i]) {
                case 1:
                  temp += "课后作业,";
                  break;
                case 2:
                  temp += "押题模考,";
                  break;
                case 3:
                  temp += "结业考试,";
                  break;
                case 4:
                  temp += "智能练习,";
                  break;
              }
            }
            element.typeValue = temp.substring(0, temp.length - 1);
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    update(id) {
      classPresentGoods(id).then((res) => {
        if (res.code == "200") {
          this.getList();
          this.$message({ message: res.msg, type: "success" });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    preview(id) {
      this.$router.push({ path: "/classDetail", query: { id: id } });
    },
    edit(id) {
      this.$router.push({ path: "/classEdit", query: { id: id } });
    },
    del(id) {
      this.$confirm("确定删除该班级吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        classDel(id).then((res) => {
          if (res.code == "200") {
            this.$message({ message: res.msg, type: "success" });
            this.getList(1);
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      });
    },
    handleDelete(id) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          testDelete(id).then((res) => {
            if (res.code == "200") {
              this.getList();
              this.$message({ message: res.msg, type: "success" });
            } else {
              this.$message({ message: res.msg, type: "error" });
            }
          });
        })
        .catch(() => {});
    },
    handleSizeChange() {},
    add() {
      this.$router.push("/classAdd");
    },
  },
};
</script>
<style scoped>
.form {
  padding-left: 26px;
  padding-right: 24px;
  padding-top: 24px;
  background: #fff;
}
.search {
  display: flex;
  position: relative;
}
.search_info {
  width: 192px;
  height: 40px;
  margin-right: 16px;
}
.searchBtn {
  margin-left: 16px;
  height: 36px;
}
.sortStyle {
  height: 19px;
  float: right;
  /* right: 24px; */
  margin-top: 20px;
  margin-bottom: 30px;
}
.pos_right_btn {
  position: absolute;
  right: 0;
}
.opt_font {
  cursor: pointer;
  color: #2e82ff;
}
.detail_title {
  font-size: 16px;
  font-weight: 400;
  color: #212121;
  line-height: 22px;
}
.detail_title span {
  padding-left: 4px;
  background: #1e88e5;
  margin-right: 12px;
}
.row {
  display: flex;
  padding-bottom: 12px;
}
.detail_info {
  padding-top: 16px;
}
.info_left {
  display: flex;
}
.info_right {
  display: flex;
  padding-left: 202px;
}
.info_left_title {
  font-size: 14px;
  font-weight: 400;
  color: #616161;
  line-height: 20px;
  width: 112px;
  margin-right: 16px;
}
.info_left_content {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 150px;
}
.pos_top {
  padding-top: 12px;
}
.detailTable {
  margin-top: 16px;
}
.opt_font {
  color: #1e88e5;
  cursor: pointer;
}
.page_pos {
  text-align: right;
  padding-top: 10px;
}
.backBtn {
  text-align: center;
  margin-top: 10px;
}
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% -30px);
  /* max-width: ; */
}
/deep/ .el-dialog .el-dialog__body {
  flex: 1;
  height: 700px;
  overflow-y: auto;
  padding-top: 0;
}
.tab {
  display: flex;
  position: absolute;
  right: 32px;
  top: 14px;
}
.tab_border {
  border: 1px solid #409eff;
  color: #409eff;
  padding: 10px 12px;
  font-size: 14px;
  margin-right: 16px;
}
.change {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}
.listTable {
  margin-top: 17px;
}
</style>
